using System;
using Cinemachine;
using UnityEngine;

namespace Yoozoo.Core.Common
{
    public class ClipPlaneQualityListener : QualityListenerMono<ClipPlaneQualityListener.ClipPlaneQualitySetting>
    {
        [Serializable]
        public class ClipPlaneQualitySetting : QualitySetting
        {
            public float near = 0.01f;
            public float far = 2000;
        }

        [SerializeField]
        private Camera _camera;
        [SerializeField]
        private CinemachineVirtualCamera _virtualCamera;
        
        protected override void Awake()
        {
            base.Awake();
            if (!_camera)
            {
                _camera = GetComponent<Camera>();
            }

            if (!_virtualCamera)
            {
                _virtualCamera = GetComponent<CinemachineVirtualCamera>();
            }
        }
        
        public override void ChangeQualitySetting(ClipPlaneQualitySetting qualitySetting)
        {
            if (_camera)
            {
                _camera.nearClipPlane = qualitySetting.near;
                _camera.farClipPlane = qualitySetting.far;
            }

            if (_virtualCamera)
            {
                _virtualCamera.m_Lens.NearClipPlane = qualitySetting.near;
                _virtualCamera.m_Lens.FarClipPlane = qualitySetting.far;
            }
        }
    }
}